package com.zhaosc.spark.sql.df

import org.apache.spark.sql.SparkSession
import org.apache.spark.SparkConf

object RDD2DataFrameByReflection {

  def main(args: Array[String]): Unit = {

    val spark = SparkSession
      .builder()
      .appName("RDD2DataFrameByReflection")
      .config("spark.master", "local")
      .getOrCreate();
    import spark.implicits._;
    val peopleDF = spark.sparkContext
      .textFile("Peoples.txt")
      .map(v => {
        val params = v.split(",");
        new Person(params(0).toInt, params(1), params(2).toInt);
      }).toDF();

    peopleDF.createOrReplaceTempView("people")
    val teenagersDF = spark.sql("SELECT name, age FROM people WHERE age BETWEEN 13 AND 19")
    teenagersDF.map(teenager => "Name: " + teenager(0)).show()
    teenagersDF.map(teenager => "Name: " + teenager.getAs[String]("name")).show()
    
    spark.sparkContext.stop();
  }
}